CREATE PROCEDURE [sp_ssiscatalog].[exec_most_recent_status]
	@status_id									INT				
,	@exec_execution								BIT					=	1	
,	@exec_events								BIT					=	1	
,	@exec_errors								BIT					=	1	
,	@exec_warnings								BIT					=	1	
,	@exec_executable_stats						BIT					=	1	
,	@exec_events_packagesexcluded				NVARCHAR(MAX)		=	NULL
,	@show_docs									BIT					=	1		
,	@show_docs_only								BIT					=	0
AS
BEGIN
		SET NOCOUNT ON;
		DECLARE	@vDocs	NVARCHAR(MAX)	=	N'Parameters:
,	@exec_execution								BIT					=	1						/*Return a one-row result set showing information about the execution*/
,	@exec_events								BIT					=	1						/*Return a result set showing all events*/
,	@exec_errors								BIT					=	1						/*Return a result set showing all errors*/
,	@exec_warnings								BIT					=	1						/*Return a result set showing all warnings*/
,	@exec_executable_stats						BIT					=	1						/*Return a result set showing all executable stats*/
,	@exec_events_packagesexcluded				NVARCHAR(MAX)		=	NULL					/*Specify a list of packages for which events should not be displayed*/
';
		DECLARE	@exec_id	BIGINT;
		DECLARE	@status_desc NVARCHAR(MAX)		=	(SELECT [status_desc] FROM [sp_ssiscatalog].[get_status_desc_from_status](@status_id))
		DECLARE	@operation_type	NVARCHAR(MAX)	=	N'exec_' + @status_desc;
		EXEC	[sp_ssiscatalog].[show_docs]
					@docs			=	@vDocs
				,	@action			=	@operation_type
				,	@show_docs		=	@show_docs;
		IF (@show_docs_only = 1)
				RETURN;
		BEGIN TRY
				SELECT	@exec_id = MAX(e.[execution_id])
				FROM	[$(SSISDB)].[catalog].[executions]e
				WHERE	e.[status] = @status_id;
				
				IF NOT EXISTS (SELECT 1 FROM [$(SSISDB)].[catalog].[executions] WHERE [execution_id] = @exec_id)
						RAISERROR('Did not find an execution with status=%s or you do not have permission to access information about such an execution',1,1,@status_desc); --Note low severity.
				ELSE
				BEGIN
						IF (@exec_execution = 1)
								SELECT	e.[ResultSet],e.[folder_name],e.[project_name],e.[package_name],e.[executed_as_name],e.[status_desc],e.[start_time],e.[end_time],e.duration_in_seconds,e.[execution_id]
								FROM	[sp_ssiscatalog].[executions](@exec_id,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT) e;
								RAISERROR('%d executions returned',1,1,@@ROWCOUNT);

						IF (@exec_events = 1)
								EXEC	[sp_ssiscatalog].[get_events]				
											@exec_id									=	@exec_id
										,	@exec_events_packagesexcluded				=	@exec_events_packagesexcluded;
								
						IF(@exec_errors = 1)
								EXEC	[sp_ssiscatalog].[get_filteredevents]		
											@exec_id	=	@exec_id
										,	@events		=	N'OnError,OnTaskFailed';

						IF(@exec_warnings = 1)
								EXEC	[sp_ssiscatalog].[get_filteredevents]		
											@exec_id	=	@exec_id
										,	@events		=	N'OnWarning';
						IF(@exec_executable_stats = 1)
								EXEC	[sp_ssiscatalog].[get_executable_stats]	
											@exec_id	=	@exec_id;
				END              
		END TRY
		BEGIN CATCH      
				THROW;
		END CATCH
END
	